﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RRRSRoguelike.Entities;
using RRRSRoguelike.Enums;
using System.Drawing;

namespace RRRSRoguelike.Managers
{
    class PropManager
    {

        #region variables
        private IList<Tile> props;


        public Tile LookTile;
        public Fireball Fireball; 
        #endregion

        public IList<Tile> Props
        {
        	get { return props;}
        }
        
        
        #region methods
        public PropManager()
        {
            props = new List<Tile>();
            Fireball = new Fireball(new Point(), Direction.N);
            LookTile = new Tile();
            
        }
        
        public void AddStairwell(Point position)
        {       	
        		
        	props.Add(new Stairwell(position, StepDirection.Up));
        	
        	PropsToTiles();
        
        }

        public void ClearProps()
        {
            props.Clear();
        }

        public void AddProps()
        {
            for (int i = 0; i < Constants.NumberOfSwords; i++)
            {
            	int index = DungeonManager.Instance.DungeonBuilderRandom.Next(
            		0,DungeonManager.Instance.Dungeon.Data.Props.Count);
                
            		Weapon weapon = new Weapon(DungeonManager.Instance.DungeonLevel.GetValidRandomPoint(true),
            	                           DungeonManager.Instance.Dungeon.Data.Props.ElementAt(index).Value);
                props.Add(weapon);
            }

            AddStairwell(DungeonManager.Instance.DungeonLevel.GetValidRandomPoint(false));

        }

        public Weapon GetBestWeapon(Point location)
        {
            Weapon weapon;
            weapon = props.OfType<Weapon>().Where(s => s.Position == location).OrderBy(s => s.UsesRemaining).Last();
            return weapon;
        }

        public void Add(Tile prop)
        {
            props.Add(prop);
        }

        public void Remove(Tile prop)
        {
            props.Remove(prop);
        }

        public void PropsToTiles()
        {
            props.ToList().ForEach(s => DungeonManager.Instance.DungeonLevel.AddTile(s,Layers.Prop));
            if (Fireball.Active) DungeonManager.Instance.DungeonLevel.AddTile(Fireball,Layers.Effect);
            if (LookTile.Active) DungeonManager.Instance.DungeonLevel.AddTile(LookTile, Layers.Effect); 
            DungeonManager.Instance.DungeonLevel.walls.ToList().ForEach(
            	w => DungeonManager.Instance.DungeonLevel.AddTile(w, Layers.Prop));
        }
        #endregion

    }
}
